package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.business.dto.request.modify.MaintenanceModifyDto;
import com.molichuxing.services.infrastructure.bean.MaintenanceBean;
import com.molichuxing.services.infrastructure.bean.MaintenanceLogBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.MaintenanceLogMapper;
import com.molichuxing.services.infrastructure.dto.request.create.MaintenanceLogCreateDto;
import com.molichuxing.services.infrastructure.dto.response.MaintenanceDto;
import com.molichuxing.services.infrastructure.dto.response.MaintenanceLogDto;
import com.molichuxing.services.infrastructure.service.MaintenanceLogService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 保养历史表 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-09-23
 */
@Service("maintenanceLogService")
public class MaintenanceLogServiceImpl implements MaintenanceLogService {

    private static final Logger log = Logger.getLogger(MaintenanceLogServiceImpl.class);

    @Resource
    private MaintenanceLogMapper maintenanceLogMapper;

    @Override
    public Paged<MaintenanceLogDto> getMaintenanceLogPage(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        log.info("根据条件查询保养列表。map ： ["+ map +"]");
        Paged<MaintenanceLogDto> paged = new Paged<>();
        int count = maintenanceLogMapper.getMaintenanceLogPageCount(map);
        if(count <= 0) {
            return paged;
        }
        List<MaintenanceLogBean> result = maintenanceLogMapper.getMaintenanceLogPage(map, pageNum, pageSize);
        log.info("根据条件查询保养列表 出参：result： "+ JSON.toJSONString(result));
        paged.setList(Convert.toMaintenanceLogDto(result));
        paged.setTotal(count);
        paged.setPageSize(pageSize);
        return paged;
    }

    @Override
    public MaintenanceLogDto getMaintenanceLogById(Integer maintenanceLogId) {
        return Convert.toMaintenanceLogDto(maintenanceLogMapper.getMaintenanceLogById(maintenanceLogId));
    }

    @Override
    public MaintenanceLogDto getMaintenanceLogByPId(Integer maintenanceLogId) {
        return Convert.toMaintenanceLogDto(maintenanceLogMapper.getMaintenanceLogByPId(maintenanceLogId));
    }

    @Override
    public Integer getCount(Map<String, Object> map) {
        return maintenanceLogMapper.getMaintenanceLogPageCount(map);
    }

    @Override
    public List<MaintenanceLogDto> getMaintenanceLogList(Integer carId, List<Integer> statusList) {
        return Convert.toMaintenanceLogDto(maintenanceLogMapper.getMaintenanceLogList(carId, statusList));
    }

    @Override
    public int createMaintenanceLog(MaintenanceLogCreateDto maintenanceLogCreateDto) {
        return maintenanceLogMapper.createMaintenanceLog(Convert.toMaintenanceLogBean(maintenanceLogCreateDto));
    }

    @Override
    public int updateById(MaintenanceLogCreateDto maintenanceLogCreateDto) {
        return  maintenanceLogMapper.updateById(Convert.toMaintenanceLogBean(maintenanceLogCreateDto));
    }

    @Override
    public Paged<MaintenanceLogDto> getMaintenanceLogAppPage(Map<String, Object> map ,Integer pageNum, Integer pageSize) {
        Paged<MaintenanceLogDto> paged = new Paged<>();
        int count = maintenanceLogMapper.getMaintenanceLogAppCount(map);
        if(count <= 0) {
            return paged;
        }
        List<MaintenanceLogBean> result = maintenanceLogMapper.getMaintenanceLogAppPage( map,pageNum, pageSize);
        paged.setList(Convert.toMaintenanceLogDto(result));
        paged.setTotal(count);
        paged.setPageSize(pageSize);
        return paged;
    }

    @Override
    public int modifyMaintenanceCheck(MaintenanceModifyDto modifyDto) {
        return maintenanceLogMapper.updateById(Convert.toModifyMaintenanceLogBean(modifyDto));
    }
}
